package com.peakmain.homemodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.homemodule.contarct.HotkeyContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;
import com.peakmain.wanandroid.netmodule.bean.home.HotkeyBean;
import com.peakmain.wanandroid.netmodule.bean.home.SearchResultBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/23
 * mail: 2726449200@qq.com
 * desiption:
 */
public class HotKeyPresenter extends BasePresenter<HotkeyContract.View> implements HotkeyContract.Presenter {
    @Inject
    public HotKeyPresenter(){

    }
    @SuppressLint("CheckResult")
    @Override
    public void getHotkeySearch() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getHotkey()
                .compose(mView.<DataResponse<List<HotkeyBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<HotkeyBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<HotkeyBean>>>() {
                    @Override
                    public void accept(DataResponse<List<HotkeyBean>> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showHotKeySuccess(dataResponse.getData());
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public void getSearchResult(int page, String key, final boolean isPullDown, final boolean isSilence) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getSearch(page,key)
                .compose(mView.<DataResponse<SearchResultBean>>bindToLife())
                .compose(RxSchedulers.<DataResponse<SearchResultBean>>applySchedulers())
                .subscribe(new Consumer<DataResponse<SearchResultBean>>() {
                    @Override
                    public void accept(DataResponse<SearchResultBean> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showSearchResultSuccess(dataResponse.getData(),isPullDown,isSilence);
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }
}
